<?php

/**
 * Controller_achieve
 教师档案
 */

class Controller_achieve extends MyApp {


	public function __construct()
	{
		parent::__construct();
        $this->model->file  = YOU::loadModel('file');
        $this->model->semester = YOU::loadModel('semester');
		$this->model->record = YOU::loadModel('record');
		$this->model->user   = YOU::loadModel('user');
        $prize = $this->db->fetchAll("SELECT * FROM `tbl_prize` ");
        $prizelv = $this->db->fetchAll("SELECT * FROM `tbl_prizelv` ");
        $this->tpl->assign('prize',$prize);
        $this->tpl->assign('prizelv',$prizelv);
        $type = $this->get->type;
        if(!$type){
            $type = 1;
        }
        $this->tpl->assign('type',$type);
        $this->type = $type;
	}

	/** 首页 **/
	public function actionIndex()
	{

	}

	/** 列表 **/
	public function actionList ()
	{
        $achieve_type =  (int)$this->type;
		//按条件搜索
		$where = "where achieve_type = ".$achieve_type;

        if($_POST){
            if($this->post->prizelv != 'all'){
                $where .=" AND achieve_lv =".$this->post->prizelv;
                $this->tpl->assign('lv',$this->post->prizelv);
            }
            if($this->post->prize !='all'){
                $where .=" AND achieve_rank =".$this->post->prize;
                $this->tpl->assign('rank',$this->post->prize);
            }
            $from = ($this->post->from);
            $to = ($this->post->to);
            if(!empty($from)){
                $where .=" AND achieve_time >= '{$from}' ";
                $this->tpl->assign("from", $from);
            }
            if(!empty($to)){
                $where .=" AND achieve_time <= '{$to}+1' ";
                $this->tpl->assign("to", $to);
            }
        }

		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_achieve`  {$where} ");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		// 非全部数据输出状态下才分页
		if(empty($this->get->output)){
			$limit = " LIMIT {$start}, ".PAGE_LIMIT;
		}else{
			$limit = "";
		}

		$achieve_lists = $this->db->fetchAll("SELECT * FROM `tbl_achieve` {$where}  ORDER BY achieve_id DESC {$limit} ");

        foreach ($achieve_lists as $key => $value){
            $achieve_lists[$key]['achieve_remark'] = mb_substr($value['achieve_remark'],0,7,'utf-8');
            if(!empty($value['achieve_lv'])) {
                $prizelv_lists = $this->db->fetchRow("SELECT `prizelv_name` FROM `tbl_prizelv` WHERE prizelv_id = " . $value['achieve_lv']);
                $achieve_lists[$key]['achieve_lv'] = $prizelv_lists['prizelv_name'];
            }

            if(!empty($value['achieve_rank'])) {
                $prize_lists = $this->db->fetchRow("SELECT `prize_name` FROM `tbl_prize` WHERE prize_id = " . $value['achieve_rank']);
                $achieve_lists[$key]['achieve_rank'] = $prize_lists['prize_name'];
            }
            $achieve_lists[$key]['achieve_file']       = unserialize($value['achieve_file']);
//            var_dump(unserialize($value['achieve_file'])) ;
//            $achieve_lists[$key]['achieve_fname']       = mb_substr(unserialize($value['achieve_file']),0,7,'utf-8');
            if(!empty($value['achieve_teach'])){
                $teach_lists = $this->db->fetchRow("SELECT `staff_name` FROM `tbl_staff` WHERE staff_id = ".$value['achieve_teach']);
                $achieve_lists[$key]['achieve_teach']       = $teach_lists['staff_name'];
            }
            if(!empty($value['achieve_teachers'])) {
                $names_lists = $this->db->fetchAll("SELECT `staff_name` FROM `tbl_staff` WHERE staff_id in (" . $value['achieve_teachers'].")");
                $names = '';
                foreach($names_lists as $k => $v){
                    $names .= $v['staff_name'].",";
                }
                $achieve_lists[$key]['achieve_teachers'] = mb_substr($names,0,7,'utf-8');
            }
            if(!empty($value['achieve_publish']) && $this->type == 6 ){
                $publish_lists = $this->db->fetchRow("SELECT `publish_name` FROM `tbl_publish` WHERE publish_id = '".$value['achieve_publish']."'");
                $achieve_lists[$key]['achieve_publish']       = $publish_lists['publish_name'];
            }
            if($this->type != 5 && $this->type != 7 && !empty($value['achieve_owner'])){//获奖者姓名转换
                $teacher_lists = $this->db->fetchRow("SELECT `staff_name` FROM `tbl_staff` WHERE staff_id = ".$value['achieve_owner']);
                //var_dump($teacher_lists);exit;
                $achieve_lists[$key]['achieve_owner']       = $teacher_lists['staff_name'];
            }
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";


		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("achieve_lists", $achieve_lists);
        $this->tpl->assign("count", $count);

		$this->tpl->display("achieve/achieve.list.tpl.php");
	}
	


	/** 添加 **/
	public function actionAdd()
	{


		if ($_POST){
            //var_dump($_POST);exit;
			$achieve_name      = $this->post->achieve_name;
			$achieve_unit    = $this->post->achieve_unit;
			$achieve_rank       = $this->post->achieve_rank;
			$achieve_lv  = $this->post->achieve_lv;
			$achieve_owner = $this->post->achieve_owner;
            $achieve_time     = $this->post->achieve_time;
            $achieve_remark      = $this->post->achieve_remark;
			$achieve_file      = $this->post->achieve_file;
            $achieve_teach      = $this->post->achieve_teach;
            $achieve_teachers        = $this->post->achieve_teachers;
            $achieve_publish       = $this->post->achieve_publish;
            $achieve_bookno    = $this->post->achieve_bookno;
            $achieve_page     = $this->post->achieve_page;
            $achieve_type     = $this->post->achieve_type;
            $achieve_trank     = $this->post->achieve_trank;
            if($achieve_teachers){
                $teachers = implode(',',$achieve_teachers);
                $achieve_teachers = $teachers;
            }

				// 写入数据库
				$info = array(
				"achieve_name"       => $achieve_name,
				"achieve_unit"     => $achieve_unit,
				"achieve_rank"        => $achieve_rank,
				"achieve_lv"   => $achieve_lv,
				"achieve_owner"  => $achieve_owner,
				"achieve_time"      => $achieve_time,
				"achieve_remark"      => $achieve_remark,
				"achieve_file"       => $achieve_file,
				"achieve_teach"      => $achieve_teach,
				"achieve_teachers"        => $achieve_teachers,
				"achieve_publish"       => $achieve_publish,
				"achieve_bookno"       => $achieve_bookno,
				"achieve_page"         => $achieve_page,
				"achieve_type"        => $achieve_type,
                 "achieve_trank"        => $achieve_trank,
                    "create_time"       =>date("Y-m-d",time()),
				);



            $this->db->do_insert("tbl_achieve", $info);
            if($this->post->same == 'on'){
                $info['achieve_type'] = 7;
                $this->db->do_insert("tbl_achieve", $info);
            }
            $achieve_id = $this->db->insert_id();
            // 附件上传
            $file = $this->model->file->upload("achieve_file", $achieve_id, array("txt","ppt", "xls", "xlsx", "docs", "doc", "pdf", "jpg", "png", "gif", "zip", "rar"));
            if (!empty($file)){
                $info = array(
                    "achieve_file" => serialize($file['fname']),
                );

                $this->db->do_update("tbl_achieve", $info, "achieve_id='{$achieve_id}'");
            }
            // 跳转
            $type = $this->type;
            Helper_Client::location("添加成功", "index.php?a=achieve.list&type=$type");
		}
// 出版社名称
        if($this->type == 6 ){
            $publish_lists = $this->db->fetchAll("SELECT `publish_name`,`publish_id` FROM `tbl_publish`");
            $this->tpl->assign("publish_lists", $publish_lists);
        }

        $teachers = $this->db->fetchAll("SELECT `staff_id`,`staff_name`,`staff_number` FROM `tbl_staff` ");
        $this->tpl->assign('teachers',$teachers);
		$this->tpl->display("achieve/achieve.add.tpl.php");
	}

	/** 修改 **/
	public function actionUpdate()
	{	
		$achieve_id   = (int)$this->get->achieve_id;
		$data = $this->db->fetchRow("SELECT * FROM `tbl_achieve` WHERE achieve_id='{$achieve_id}'");
        if (empty($data)){
			Helper_Client::alert("该奖不存在");
		}
        $uid = $this->user['user_id'];

        if($data['user_id']  && ($uid !=$data['achieve_owner']) ){
            Helper_Client::alert("对不起无此权限");
            exit;
        }

        if ($_POST) {

            $achieve_name = $this->post->achieve_name;
            $achieve_unit = $this->post->achieve_unit;
            $achieve_rank = $this->post->achieve_rank;
            $achieve_lv = $this->post->achieve_lv;
            $achieve_owner = $this->post->achieve_owner;
            $achieve_time = $this->post->achieve_time;
            $achieve_remark = $this->post->achieve_remark;
            $achieve_file = $this->post->achieve_file;
            $achieve_teach = $this->post->achieve_teach;
            $achieve_teachers = $this->post->achieve_teachers;
            $achieve_publish = $this->post->achieve_publish;
            $achieve_bookno = $this->post->achieve_bookno;
            $achieve_page = $this->post->achieve_page;
            $achieve_type = $this->post->achieve_type;
            $achieve_trank = $this->post->achieve_trank;
            if($achieve_teachers){
                $teachers = implode(',',$achieve_teachers);
                $achieve_teachers = $teachers;
            }

                // 写入数据库
                $info = array(
                    "achieve_name" => $achieve_name,
                    "achieve_unit" => $achieve_unit,
                    "achieve_rank" => $achieve_rank,
                    "achieve_lv" => $achieve_lv,
                    "achieve_owner" => $achieve_owner,
                    "achieve_time" => $achieve_time,
                    "achieve_remark" => $achieve_remark,
                    "achieve_file" => $achieve_file,
                    "achieve_teach" => $achieve_teach,
                    "achieve_teachers" => $achieve_teachers,
                    "achieve_publish" => $achieve_publish,
                    "achieve_bookno" => $achieve_bookno,
                    "achieve_page" => $achieve_page,
                    "achieve_type" => $achieve_type,
                    "achieve_trank" => $achieve_trank,
                );
                // 附件上传
                if ($this->post->achieve_file_re=='1'){
                    // 附件上传
                    $file = $this->model->file->upload("achieve_file", $achieve_id, array("txt","ppt", "xls", "xlsx", "docs", "doc", "pdf", "jpg", "png", "gif", "zip", "rar"));

                    if (!empty($file)){
                        $info = array(
                            "achieve_file" => $file['fname'],
                        );

                        $this->db->do_update("tbl_achieve", $info, "achieve_id='{$achieve_id}'");
                    }
                }
//                // 附件上传
//                $file = $this->model->file->upload("achieve_file", $achieve_id, array("txt","ppt", "xls", "xlsx", "docs", "doc", "pdf", "jpg", "png", "gif", "zip", "rar"));
//                if (!empty($file)){
//
//                    $info['achieve_file'] = $file['fname'];
//
//                }




//yuan
            $this->db->do_update("tbl_achieve", $info, "achieve_id='{$achieve_id}'");
            if ($this->post->same == 'on') {
                $info['achieve_type'] = 7;
                $achieve_id++;
                $this->db->do_update("tbl_achieve", $info, "achieve_id='{$achieve_id}'");
            }

            // 跳转
            $type = $this->type;
            Helper_Client::location("修改成功", "index.php?a=achieve.list&type=$type");

        }
        $publish_lists = $this->db->fetchAll("SELECT `publish_name`,`publish_id` FROM `tbl_publish`");
        $this->tpl->assign("publish_lists", $publish_lists);
        $teachers = $this->db->fetchAll("SELECT `staff_id`,`staff_name`,`staff_number` FROM `tbl_staff` ");
        $this->tpl->assign('teachers',$teachers);
        $data['achieve_teachers'] = explode(',',$data['achieve_teachers']);
		$this->tpl->assign("data", $data);
		$this->tpl->display("achieve/achieve.update.tpl.php");
	}

	/** 删除 **/
	public function actionDel ()
	{
		$achieve_id = (int)$this->get->achieve_id;
        $data = $this->db->fetchRow("SELECT * FROM `tbl_achieve` WHERE achieve_id='{$achieve_id}'");
        if (empty($data)){
            Helper_Client::alert("该奖不存在");
        }
        $uid = $this->user['user_id'];

        if($data['user_id']  && ($uid !=$data['achieve_owner']) ){
            Helper_Client::alert("对不起无此权限");
            exit;
        }
		$this->db->query("DELETE  FROM `tbl_achieve` WHERE achieve_id='{$achieve_id}'");
		Helper_Client::location("删除成功", "index.php?a=achieve.list");
	}

	//------------------------------------------------------------------------------//
    /** 查看**/
    public function actionInfo()
    {
        $achieve_id   = (int)$this->get->achieve_id;
        $data = $this->db->fetchRow("SELECT * FROM `tbl_achieve` WHERE achieve_id='{$achieve_id}'");
        if (empty($data)){
            Helper_Client::alert("该奖不存在");
        }
        if($data['achieve_lv']){
        $prizelv_lists = $this->db->fetchRow("SELECT `prizelv_name` FROM `tbl_prizelv` WHERE prizelv_id = ".$data['achieve_lv']);
        $data['achieve_lv']       = $prizelv_lists['prizelv_name'];
        }
        if($data['achieve_rank']){
        $prize_lists = $this->db->fetchRow("SELECT `prize_name` FROM `tbl_prize` WHERE prize_id = ".$data['achieve_rank']);
        $data['achieve_rank']       = $prize_lists['prize_name'];
        }
        if(!empty($data['achieve_publish'])){
            $publish_lists = $this->db->fetchRow("SELECT `publish_name` FROM `tbl_publish` WHERE publish_id = ".$data['achieve_publish']);
            $data['achieve_publish']       = $publish_lists['publish_name'];
        }

        $data['achieve_file'] = unserialize($data['achieve_file']);
        if(!empty($data['achieve_teach'])){
            $teach_lists = $this->db->fetchRow("SELECT `staff_name` FROM `tbl_staff` WHERE staff_id = ".$data['achieve_teach']);
            $data['achieve_teach']       = $teach_lists['staff_name'];
        }

        if($this->type != 5 && $this->type != 7 && $data['achieve_owner']){//获奖者姓名转换
            $teacher_lists = $this->db->fetchRow("SELECT `staff_name` FROM `tbl_staff` WHERE staff_id = ".$data['achieve_owner']);
            $data['achieve_owner']       = $teacher_lists['staff_name'];
        }
        if(!empty($data['achieve_teachers'])) {
            $names_lists = $this->db->fetchAll("SELECT `staff_name` FROM `tbl_staff` WHERE staff_id in (" . $data['achieve_teachers'].")");
            $names = '';
            foreach($names_lists as $k => $v){
                $names .= $v['staff_name'].",";
            }
            $data['achieve_teachers'] = mb_substr($names,0,7,'utf-8');
        }

        $this->tpl->assign("data", $data);
        $this->tpl->display("achieve/achieve.info.tpl.php");
    }

    public function actionChecks(){
        $name = $this->post->name;
        $data = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_student` WHERE student_name = '{$name}'");
        echo $data['count'];
    }

    /** 文件下载 **/
    public function actionFile()
    {
        $achieve_id = (int)$this->get->achieve_id;
        $fname   = $this->get->fname;

        $achieve_data = $this->db->fetchRow("SELECT * FROM `tbl_achieve` WHERE achieve_id='{$achieve_id}' ");
        if (empty($achieve_data)){
            Helper_Client::alert("不存在");
        }

        // 展开附件
        if (!empty($achieve_data['achieve_file'])){
            $achieve_data['achieve_file'] = unserialize($achieve_data['achieve_file']);
            //foreach($achieve_data['achieve_file'] as $k => $v){
                //if ($fname == $v['fname']){

                    $fpath = UPLOAD_PATH."/achieve_file/{$fname}";
                    //echo $fpath ; exit;
                    // 输出下载文件头
                    header("Content-Disposition: attachment; filename={$fname}");
                    // 在线输出
                    readfile($fpath);

                //}
           // }
        }else{
            Helper_Client::alert("附件不存在");
        }
    }

}

?>